1 Imports System.Data.SqlClient
2 Public Class frmClassType
3
4 Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
5 Me.Close()
6 End Sub
7 Sub Reset()
8 txtClassType.Text = ""
9 btnSave.Enabled = True
10 btnDelete.Enabled = False
11 btnUpdate.Enabled = False
12 txtClassType.Focus()
13 End Sub
14 Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
15 Reset()
16 End Sub
17
18 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
19 If txtClassType.Text = "" Then
20 MessageBox.Show("Please enter Class type", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
21 txtClassType.Focus()
22 Return
23 End If
24
25 Try
26 con = New SqlConnection(cs)
27 con.Open()
28 Dim ct As String = "select type from ClassType where type=@d1"
29 cmd = New SqlCommand(ct)
30 cmd.Parameters.AddWithValue("@d1", txtClassType.Text)
31 cmd.Connection = con
32 rdr = cmd.ExecuteReader()
33
34 If rdr.Read() Then
35 MessageBox.Show("Class Type Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
36 txtClassType.Text = ""
37 txtClassType.Focus()
38 If (rdr IsNot Nothing) Then
39 rdr.Close()
40 End If
41 Return
42 End If
43
44 con = New SqlConnection(cs)
45 con.Open()
46
47 Dim cb As String = "insert into ClassType(type) VALUES (@d1)"
48 cmd = New SqlCommand(cb)
49 cmd.Parameters.AddWithValue("@d1", txtClassType.Text)
50 cmd.Connection = con
51 cmd.ExecuteReader()
52 con.Close()
53 Dim st As String = "added the new Class type '" & txtClassType.Text & "'"
54 LogFunc(lblUser.Text, st)
55 MessageBox.Show("Successfully Saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
56 btnSave.Enabled = False
57 Getdata()
58 Catch ex As Exception
59 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
60 End Try
61 End Sub
62
63 Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
64 If txtClassType.Text = "" Then
65 MessageBox.Show("Please enter Class type", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
66 txtClassType.Focus()
67 Return
68 End If
69
70 Try
71
72 con = New SqlConnection(cs)
73 con.Open()
74
75 Dim cb As String = "Update ClassType set type=@d1 where type=@d2"
76 cmd = New SqlCommand(cb)
77 cmd.Connection = con
78 cmd.Parameters.AddWithValue("@d1", txtClassType.Text)
79 cmd.Parameters.AddWithValue("@d2", txtType.Text)
80 cmd.ExecuteReader()
81 con.Close()
82 Dim st As String = "updated the Class type '" & txtClassType.Text & "'"
83 LogFunc(lblUser.Text, st)
84 MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
85 btnUpdate.Enabled = False
86 Getdata()
87 Catch ex As Exception
88 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
89 End Try
90 End Sub
91 Private Sub DeleteRecord()
92
93 Try
94 Dim RowsAffected As Integer = 0
95 con = New SqlConnection(cs)
96 con.Open()
97 Dim cl As String = "select Type from Class,ClassType where Class.ClassType=ClassType.Type and Type=@d1"
98 cmd = New SqlCommand(cl)
99 cmd.Connection = con
100 cmd.Parameters.AddWithValue("@d1", txtClassType.Text)
101 rdr = cmd.ExecuteReader()
102 If rdr.Read Then
103 MessageBox.Show("Unable to delete..Already in use in Class Entry", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
104 If Not rdr Is Nothing Then
105 rdr.Close()
106 End If
107 Exit Sub
108 End If
109 con = New SqlConnection(cs)
110 con.Open()
111 Dim cl1 As String = "select Type from Staff,ClassType where Staff.ClassType=ClassType.Type and Type=@d1"
112 cmd = New SqlCommand(cl1)
113 cmd.Connection = con
114 cmd.Parameters.AddWithValue("@d1", txtClassType.Text)
115 rdr = cmd.ExecuteReader()
116 If rdr.Read Then
117 MessageBox.Show("Unable to delete..Already in use in Staff Entry", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
118 If Not rdr Is Nothing Then
119 rdr.Close()
120 End If
121 Exit Sub
122 End If
123 con = New SqlConnection(cs)
124 con.Open()
125 Dim cq As String = "delete from ClassType where type=@d1"
126 cmd = New SqlCommand(cq)
127 cmd.Parameters.AddWithValue("@d1", txtType.Text)
128 cmd.Connection = con
129 RowsAffected = cmd.ExecuteNonQuery()
130 If RowsAffected > 0 Then
131 Dim st As String = "deleted the Class type '" & txtClassType.Text & "'"
132 LogFunc(lblUser.Text, st)
133 MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
134 Getdata()
135 Reset()
136 Else
137 MessageBox.Show("No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)
138 Reset()
139 End If
140 If con.State = ConnectionState.Open Then
141 con.Close()
142
143 End If
144 Catch ex As Exception
145 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
146 End Try
147 End Sub
148 Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
149 Try
150 If MessageBox.Show("Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
151 DeleteRecord()
152 End If
153 Catch ex As Exception
154 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
155 End Try
156 End Sub
157
158 Private Sub dgw_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgw.MouseClick
159 Try
160 Dim dr As DataGridViewRow = dgw.SelectedRows(0)
161 txtType.Text = dr.Cells(0).Value.ToString()
162 txtClassType.Text = dr.Cells(0).Value.ToString()
163 btnUpdate.Enabled = True
164 btnDelete.Enabled = True
165 btnSave.Enabled = False
166 Catch ex As Exception
167 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
168 End Try
169 End Sub
170
171 Private Sub dgw_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgw.RowPostPaint
172 Dim strRowNumber As String = (e.RowIndex + 1).ToString()
173 Dim size As SizeF = e.Graphics.MeasureString(strRowNumber, Me.Font)
174 If dgw.RowHeadersWidth < Convert.ToInt32((size.Width + 20)) Then
175 dgw.RowHeadersWidth = Convert.ToInt32((size.Width + 20))
176 End If
177 Dim b As Brush = SystemBrushes.ControlText
178 e.Graphics.DrawString(strRowNumber, Me.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2))
179
180 End Sub
181 Public Sub Getdata()
182 Try
183 con = New SqlConnection(cs)
184 con.Open()
185 cmd = New SqlCommand("SELECT RTRIM(type) from ClassType order by type", con)
186 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
187 dgw.Rows.Clear()
188 While (rdr.Read() = True)
189 dgw.Rows.Add(rdr(0))
190 End While
191 con.Close()
192 Catch ex As Exception
193 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
194 End Try
195 End Sub
196
197 Private Sub frmtype_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
198 Getdata()
199 End Sub
200 End Class